package com.ruoyi.web.wh.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.framework.security.SecurityUtils;
import com.ruoyi.web.wh.domain.WhRecord;
import com.ruoyi.web.wh.service.IWhRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.Log;
import com.ruoyi.framework.enums.BusinessType;
import com.ruoyi.web.wh.domain.WhGoods;
import com.ruoyi.web.wh.service.IWhGoodsService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.Result;
import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 物品Controller
 *
 * @author ruoyi
 * @date 2025-02-23
 */
@RestController
@RequestMapping("/wh/goods")
public class WhGoodsController extends BaseController {
    @Autowired
    private IWhGoodsService whGoodsService;

    @Autowired
    private IWhRecordService whRecordService;

    /**
     * 查询物品列表
     */
    @SaCheckPermission("wh:goods:list")
    @GetMapping("/list")
    public TableDataInfo list(WhGoods whGoods) {
        startPage();
        List<WhGoods> list = whGoodsService.selectWhGoodsList(whGoods);
        return getDataTable(list);
    }


    /**
     * 获取物品详细信息
     */
    @SaCheckPermission("wh:goods:query")
    @GetMapping(value = "/{id}")
    public Result getInfo(@PathVariable("id") Long id) {
        return success(whGoodsService.selectWhGoodsById(id));
    }

    /**
     * 新增物品
     */
    @SaCheckPermission("wh:goods:add")
    @Log(title = "物品", businessType = BusinessType.INSERT)
    @PostMapping
    public Result add(@RequestBody WhGoods whGoods) {
        int result = whGoodsService.insertWhGoods(whGoods);
        if (result > 0) {
            // 记录出入库记录
            WhRecord whRecord = new WhRecord();
            whRecord.setInOutType("0");
            whRecord.setGoods(whGoods.getId());
            whRecord.setUserId(SecurityUtils.getLoginUser().getUserId());
            whRecord.setCount(whGoods.getCount());
            whRecord.setCurrCount(whGoods.getCount());
            whRecordService.insertWhRecord(whRecord);
        }
        return toAjax(result);
    }

    /**
     * 修改物品
     */
    @SaCheckPermission("wh:goods:edit")
    @Log(title = "物品", businessType = BusinessType.UPDATE)
    @PutMapping
    public Result edit(@RequestBody WhGoods whGoods) {
        return toAjax(whGoodsService.updateWhGoods(whGoods));
    }


    /**
     * 修改物品
     */
    @SaCheckPermission("wh:goods:edit")
    @Log(title = "物品", businessType = BusinessType.UPDATE)
    @PostMapping("/input")
    public Result input(@RequestBody WhGoods whGoods) {
        WhGoods selWhGoods = whGoodsService.selectWhGoodsById(whGoods.getId());
        if (selWhGoods == null) {
            return error("物品不存在");
        }

        if (whGoods.getCount() == null || whGoods.getCount() <= 0) {
            return error("数量错误");
        }

        selWhGoods.setCount(selWhGoods.getCount() + whGoods.getCount());

        //
        int result = whGoodsService.updateWhGoods(selWhGoods);

        if (result > 0) {
            // 记录出入库记录
            WhRecord whRecord = new WhRecord();
            whRecord.setInOutType("0");
            whRecord.setGoods(whGoods.getId());
            whRecord.setUserId(SecurityUtils.getLoginUser().getUserId());
            whRecord.setCount(whGoods.getCount());
            whRecord.setCurrCount(selWhGoods.getCount());
            whRecordService.insertWhRecord(whRecord);
        }
        return toAjax(result);
    }

    /**
     * 修改物品
     */
    @SaCheckPermission("wh:goods:edit")
    @Log(title = "物品", businessType = BusinessType.UPDATE)
    @PostMapping("/output")
    public Result output(@RequestBody WhGoods whGoods) {
        WhGoods selWhGoods = whGoodsService.selectWhGoodsById(whGoods.getId());
        if (selWhGoods == null) {
            return error("物品不存在");
        }

        if (whGoods.getCount() == null || whGoods.getCount() <= 0) {
            return error("数量错误");
        }

        selWhGoods.setCount(selWhGoods.getCount() - whGoods.getCount());

        //
        int result = whGoodsService.updateWhGoods(selWhGoods);

        if (result > 0) {
            // 记录出入库记录
            WhRecord whRecord = new WhRecord();
            whRecord.setInOutType("1");
            whRecord.setGoods(whGoods.getId());
            whRecord.setUserId(SecurityUtils.getLoginUser().getUserId());
            whRecord.setCount(whGoods.getCount());
            whRecord.setCurrCount(selWhGoods.getCount());
            whRecordService.insertWhRecord(whRecord);
        }
        return toAjax(result);
    }


    /**
     * 删除物品
     */
    @SaCheckPermission("wh:goods:remove")
    @Log(title = "物品", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public Result remove(@PathVariable Long[] ids) {
        return toAjax(whGoodsService.deleteWhGoodsByIds(ids));
    }
}
